package com.jaygoo.selector;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.davidecirillo.multichoicerecyclerview.MultiChoiceAdapter;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * ================================================
 * 作    者：JayGoo
 * 版    本：1.1.0
 * 创建日期：2017/2/21
 * 描    述:多选适配器
 * ================================================
 */
public class MultiSelectListAdapter extends MultiChoiceAdapter<MultiSelectListAdapter.ChoiceViewHolder> {

    private ArrayList<String> choiceNames = new ArrayList<>();
    private OnSelectChangeListener mOnSelectChangeListener;
    private Boolean[] selectStates;
    /**
     * when you select the choices,it will be called
     */
    public interface OnSelectChangeListener{
        void onChanged(int selectedNumber);
    }


    public MultiSelectListAdapter(ArrayList<String> list){
        choiceNames = list;
        if (choiceNames != null && choiceNames.size() >0){
            selectStates = new Boolean[choiceNames.size()];
            Arrays.fill(selectStates , false);
        }
    }

    @Override
    public ChoiceViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ChoiceViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.muli_select_list_item,parent,false));
    }

    @Override
    public void onBindViewHolder(final ChoiceViewHolder holder, final int position) {
        super.onBindViewHolder(holder, position);
        // change the state
        holder.choiceNameBtn.setSelected(selectStates[position]);
        holder.choiceNameBtn.setText(choiceNames.get(position));
        holder.choiceNameBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnSelectChangeListener != null){
                    for (int i = 0; i < selectStates.length; i++) {
                        if(i == position){
                            selectStates[i] = true;
                        }else{
                            selectStates[i] = false;
                        }
                    }
                    holder.choiceNameBtn.setSelected(true);
                    mOnSelectChangeListener.onChanged(position);
                    notifyDataSetChanged();
                }

            }
        });
    }


    @Override
    public int getItemCount() {
        return choiceNames.size();
    }



    public void setOnSelectChangeListener(OnSelectChangeListener listener){
        mOnSelectChangeListener = listener;
    }

    /**
     * viewHolder
     */
    public class ChoiceViewHolder extends RecyclerView.ViewHolder {

        public TextView choiceNameBtn;

        public ChoiceViewHolder(View itemView) {
            super(itemView);
            choiceNameBtn = (TextView) itemView.findViewById(R.id.choiceNameBtn);
        }
    }
}
